﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Diagnostics;
using Aggregation_Clustering;
using System.IO;
using System.Collections;

namespace wpfAlgorithm
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public string filepath;
        public int num = 0;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "文档(*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() == true)
            {
                if (openFileDialog1.FileName != "")
                {
                    filepath = openFileDialog1.FileName;
                    this.textBox1.Text = filepath;
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "" ||textBox2.Text == ""||textBox3.Text == "")
            {
                MessageBox.Show("Please complete input!");
                return;
            }
            ClusterList clusterlist = new ClusterList();
            StreamReader sr = new StreamReader(filepath);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                string[] sarray = line.Split(' ');
                Element element =  new Element();
                element.AttributesX.Add(sarray[1]);
                element.AttributesX.Add(sarray[2]);
                element.setElementID(Convert.ToInt32(sarray[0]));
                clusterlist.AddElement2defaultCluster(element);
                num++;
            }

            clusterlist.InitClusters(Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text), 2);
            if (sr != null) sr.Close();
            clusterlist.Create2EleClusters();
            //对象的文件, 每条信息包括:ID, 所处cluster的编号, X 坐标, Y 坐标



            FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate);
            using (StreamWriter sw = new StreamWriter(aFile) )
            {
                sw.WriteLine("output.txt"+'\n');
                for (int i= 1;i<=num;i++)
                {
                    sw.WriteLine("ElementID:"+clusterlist.getElementbyID(i).ElementID.ToString() +" ; ClusterID:"+clusterlist.getElementbyID(i).ClusterID.ToString()+'\n');
                }
                sw.WriteLine("-999");
            }	
            //cluster文件:cluster的编号, 总的对象个数, 实际的总容量
            //第一行是文件名
            //第二行是需要cluster的元素的个数, 可用的clutser的数目,
            //和惩罚值 (我不是很清楚这事什么,Fred的文章中似乎没有提到, 我还在问)
            //第三行开始是相应的数据, 是一个点到另外一个点的权重或是距离
            //最后一行-999标明结束

        }
    }
}
